package www.test.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import www.test.bean.ShipInfo;
import www.test.service.ShipService;

public class InfoServlet extends HttpServlet 
{

	
	public ShipInfo domain = new ShipInfo();
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String method = request.getParameter("method");
		if(null==method)
		{
			try
			{
				this.execute(request,response);
			}
			catch (SQLException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		if("edit".equals(method))
		{
			try
			{
				this.edit(request,response);
			}
			catch (SQLException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if("add".equals(method))
		{
			this.add(request,response);
		}
		if("delete".equals(method))
		{
			try
			{
				this.delete(request,response);
			}
			catch (SQLException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if("save".equals(method))
		{
			try
			{
				this.save(request,response);
			}
			catch (SQLException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		
	}

	
	private void save(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, ServletException
	{
		domain.setId(request.getParameter("id"));
		domain.setShipName(request.getParameter("shipname"));
		domain.setVoyage(request.getParameter("voyage"));
		domain.setCategory(request.getParameter("category"));
		boolean  res =  ShipService.doSave(request,response,domain);
		if(res)
		{
			response.sendRedirect(request.getContextPath()+"/servlet/InfoServlet");
		}
		else
		{
			request.setAttribute("msg", "保存失败！");
			request.getRequestDispatcher("/info/add.jsp").forward(request, response);
		}
		
	}


	private void delete(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException
	{
		domain.setId(request.getParameter("id"));
		boolean res= ShipService.delete(request,response,domain);
		if(res)
		{
			request.setAttribute("msg", "删除成功");
			response.sendRedirect(request.getContextPath()+"/servlet/InfoServlet");
		}
		
	}


	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		request.getRequestDispatcher("/info/add.jsp").forward(request, response);
		
	}


	private void edit(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException
	{
		domain.setId(request.getParameter("id"));
		HashMap<String, ?> single = ShipService.getShipById(request,response,domain);
		request.setAttribute("single", single);
		request.setAttribute("id", request.getParameter("id"));
		request.getRequestDispatcher("/info/edit.jsp").forward(request, response);
		
		
	}


	private void execute(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException
	{
		
		if(request.getParameter("currentPage")!=null)
		{
			domain.setCurrentPage(Integer.parseInt(String.valueOf(request.getParameter("currentPage"))));
		}
		if(request.getParameter("sort")!=null)
		{
			domain.setSort(String.valueOf(request.getParameter("sort")));
		}
		if(request.getParameter("type")!=null)
		{
			domain.setType(String.valueOf(request.getParameter("type")));
		}
		
		if(request.getParameter("searchName")!=null)
		{
			domain.setSearchName(String.valueOf(request.getParameter("searchName")));
		}
		if(request.getParameter("searchVoyage")!=null)
		{
			domain.setSearchVoyage(String.valueOf(request.getParameter("searchVoyage")));
		}
		if(request.getParameter("searchCategory")!=null)
		{
			domain.setSearchCategory(String.valueOf(request.getParameter("searchCategory")));
		}
		List<HashMap<String, ?>> shipInfo = ShipService.getShipInfoList(request,response,domain);
		request.setAttribute("shipInfo", shipInfo);
		request.setAttribute("pageSize", domain.getPageSize());
		request.setAttribute("currentPage", domain.getCurrentPage());
		request.setAttribute("recordCount", domain.getRecordCount());
		request.setAttribute("pages", domain.getPageCount());
		request.setAttribute("sort", domain.getSort());
		request.setAttribute("type", domain.getType());
		request.getRequestDispatcher("/info/list.jsp").forward(request, response);
		
		
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{

		doGet(request, response);
	}

}
